#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

class Solution {
public:
    int candy(vector<int>& ratings) {
        vector<int> T(ratings.size(),1);
        int result = 0;
        for(int i = 1;i<ratings.size();i++){
            if(ratings[i]>ratings[i-1]){
                T[i] = T[i-1]+1;
            }
        }

        for(int i = ratings.size()-2;i>=0;--i ){
            if(ratings[i]>ratings[i+1]&&T[i]<=T[i+1]){
                T[i] = T[i+1]+1;
            }
        }
        
        for(int i: T){
            result += i;
        }
        return result;
    }
};

int main(){
    vector<int> ratings = {1,3,2,2,1};
    Solution S;
    cout<<S.candy(ratings)<<endl;
}